/**************************************************************************
* FXSerial
* Copyright (C) 2010 thierry lorthiois (lorthiois@bbsoft.fr)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
**************************************************************************/
/***************************************************************************
 * synchronous serial management
 * - configure : port, speed, parity, data, stop
 * - read/write data with time out
 * - control RTS signal (ready to send)
 * - read/write fixed line and control it with checksum/ACK
 *
 ***************************************************************************/

#ifndef FXSERIAL_H
#define FXSERIAL_H

#include "fx.h"

#ifdef WIN32
#include "windows.h"
#else
// Linux use the same value as WIN32
#define NOPARITY 0
#define ODDPARITY 1
#define EVENPARITY 2
#define ONESTOPBIT 0
#define ONE5STOPBITS 1
#define TWOSTOPBITS 2
#endif

#define SERIAL_DEFAULT_TIMEOUT    700



class FXSerial
{
public:
	FXSerial();
	~FXSerial();

protected:
#ifdef WIN32
	HANDLE handle;
#else
	int handle;
	struct timeval serie_timeout;
#endif
	FXString port;
	int timeout;
	bool ok;

public:
	// configure serial
	// port : in linux notation ["ttyS0", "ttyS1", "ttyS2", ...] even under Windows
	// parity : [NOPARITY, ODDPARITY, EVENPARITY]
	// stopbit : [ONESTOPBIT, ONE5STOPBITS, TWOSTOPBITS]
	bool create(const FXString &_port, int _bauds, int _parity, int _stopbit, int _databit, int _timeout=SERIAL_DEFAULT_TIMEOUT);

	// emit and receive one line with time out
	bool send_data(const char *data, size_t data_len);
	int recv_data(char *data, size_t data_len);

	void destroy();
	bool isok();
	bool clean_buffer();

	// activate/desactivate RTS signal (ready to send)
	void setRTS(bool state);

	// Read fixed size data. Control validity with Checksum and ACK.
	// 'size' is the number of character in the line (whithout checksum).
	// Need "sizeof(buffer) >= size+1" because receiveLine() will receive the checksum character.
	// On success, receiveLine send the checksum as an ACK.
	// return true : line is in 'buffer' ended with 0.
	// return false : time out after 3 try.
	bool receiveLine(char *buffer, int size);

	// Send fixed size data. Control validity with Checksum and ACK.
	// 'size' is the number of character in the line (whithout checksum).
	// Need "sizeof(buffer) >= size+1" because sendLine() will add the checksum character.
	// After sending the line, sendLine() wait for the checksum as an ACK.
	// return true : on success.
	// return false : time out after 3 try.
	bool sendLine(char *buffer, int size);

};

#endif

